// pages/admin/department/departAddStepTwo/departAddStepTwo.js
const app = getApp();
import Api from '../../../../utils/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    departName: '', //部门名称
    adminName: '', //负责人
    createTime: '', //创建时间
    did: null, //部门id
    result: null, //个人二维编号\
    userinfo:'',//个人信息
  },

  //绑定扫码
  sacnCode: function () {
    var _this = this;
    console.log(_this.data)
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        console.log("扫码结果");
        console.log(res);
        var result = res.result;
        _this.setData({
          result: res.result,
        })
        _this.getuserInfo();
        
      },
      fail: (res) => {
        console.log(res);
      }
    })
  },

  //提交绑定
  formSubmit: function (e) {
    var that = this;
    that.addHead();
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var departName = options.name;
    var createTime = options.time;
    wx.setNavigationBarTitle({
      title: departName,
    })
    this.setData({
      departName: departName,
      did: options.did,
      options: options,
      createTime: createTime,
      adminName: this.data.result,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  //个人信息
  getuserInfo: function () {
    var that = this;
    var param = {
      uid: parseInt(that.data.result),
      user_type: 1,
    };
    Api.ajax("/app/Zindex/personInfo", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          userinfo: res.data
        })
      }
    })
  },
  //添加部门负责人
  addHead: function () {
    var that = this;
    console.log(that.data);
    var param = {
      did: parseInt(that.data.options.did),
      user_id: parseInt(that.data.result),
    }
    console.log(param)
    Api.ajax('/app/Zmanage/addHead', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '绑定成功',
          icon: 'success',
          duration: 2000,
        });
        wx.reLaunch({
          url: '/pages/admin/department/departIndex/departIndex',
        })
      } else if (res.code == 205) {
        wx.showToast({
          title: '该用户未认证',
          icon: 'none',
          duration: 2000
        });
      } else if (res.code == 204){
        wx.showToast({
          title: '该用户已经是管理员',
          icon: 'none',
          duration: 2000
        });
      }else{
        wx.showToast({
          title: '认证失败',
          icon: 'none',
          duration: 2000
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})